<template>
  <el-card style="width: 100%;margin-top: 20px;">
    <template #header>
      <div class="card-header">
        <span>审核意见</span>
      </div>
    </template>
    <el-form :model="state.form" label-width="auto" style="width: 100%">
      <el-form-item label="意见快选:">
        <el-select v-model="state.form.authType" placeholder="请选择常用审批意见">
          <el-option :label="item.label" :value="item.value" @click="click(index)"
                     v-for="(item,index) of authTypeListBuild" :key="index"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="title">
        <el-input v-model="state.form.remarks" type="textarea" />
      </el-form-item>
      <el-form-item label="驳回至历史节点" v-if="state.form.authType === 4">
        <el-select v-model="state.form.rejectToNode" placeholder="请选择节点进行驳回处理">
          <el-option :label="item.displayName" :value="item.taskId" v-for="(item,index) in props.hisNodeList" :key="index"/>
        </el-select>
      </el-form-item>
    </el-form>

    <div v-if="[1,2].indexOf(state.form.authType) > -1 && props.otherConfig && props.otherConfig.actorWay === 1">
      <div v-if="props.otherConfig.actorWayType !== 5">
        <el-divider></el-divider>
        <assigner-select  title="下一审批节点指定审批人员类型(若不指定的话，流转到对应节点已配置的人员身上)"
                          ref="assigner" :otherConfig="props.otherConfig"></assigner-select>
      </div>
<!--      还原页面指定审核人员 actorWayType === 5的情况下-->
<!--      {{props.configFromCus}}-->
      <div v-if="props.configFromCus && setTypeMap[props.configFromCus.setType]?.key">
        <el-divider></el-divider>
        <assigner-select  title="下一审批节点指定审批人员类型(若不指定的话，流转到对应节点已配置的人员身上)"
                          ref="assigner" :otherConfig="props.otherConfig"
                          :setTypes="[props.configFromCus?.setType]"
                          :userListScope="props.configFromCus?.nodeUserListScope"></assigner-select>
      </div>
    </div>
    <div v-if="[6,7].indexOf(state.form.authType) > -1">
      <el-divider></el-divider>
      <assigner-select  :title="`请选择${state.form.authType === 6? '转办':'委派'}人员`" ref="transferOrDelegate"
                       :setTypes="[1]" :examineModeFlag="false" :hasOnlyOne="true"></assigner-select>
    </div>

    <div v-if="[8,9].indexOf(state.form.authType) > -1">
      <el-divider></el-divider>
      <assigner-select  :title="`请选择审批人员`" ref="appendNode"
                        :setTypes="[1]" :examineModeFlag="false" :hasOnlyOne="true"></assigner-select>
    </div>

  </el-card>
</template>
<script lang="ts" setup name="authRemarks">
import assignerSelect from './assignerSelect.vue'
import {
  defineAsyncComponent,
  ref,
  reactive,
  onMounted,
  nextTick,
  computed,
  watch,
  inject
} from "vue";
const emits = defineEmits(["update:taskState","update:eventType"]);
const authTypeList = inject('authTypeList',[]);
const title = ref('审批意见:');
const assigner = ref(null);
const transferOrDelegate = ref(null);
const appendNode = ref(null);
import { ElMessage } from 'element-plus';
const setTypeMap = {
  1: {title: '人员',key: 'nodeUserList'},
  3: {title: '角色',key: 'nodeRoleList'},
  8: {title: '组织',key: 'nodeOrgList'},
}
const props = defineProps({
  //驳回的历史节点
  hisNodeList: {
    type: Array,
    required: false,
    default: ()=> []
  },
  hasOperationData: {
    type: Array,
    required: false,
    default: ()=> ['complete','forceComplete','reject','revoke','transfer','delegate']
  },
  //流程设计里的审核人员配置数据
  otherConfig: {
    type: Object,
    required: false,
    default: ()=> {
      return null;
    }
  },
  //从还原页面获取到的审核人员配置数据
  configFromCus: {
    type: Object,
    required: false,
    default: ()=> {
      return null;
    }
  }
})

// const authTypeList = reactive([{
//   value: 1,label: '已阅,同意',eventType:'complete',title: '审批意见:'
// },{
//   value: 2,label: '已修改,请领导审批',eventType:'complete',title: '审批意见:'
// },{
//   value: 3,label: '已阅,不同意',eventType:'forceComplete',title: '审批意见:'
// },{
//   value: 4,label: '已阅,驳回',eventType:'reject',title: '驳回原因:'
// },{
//   value: 5,label: '已阅,撤销审批',eventType:'revoke',title: '撤销理由:'
// },{
//   value: 6,label: '已阅,转办',eventType:'transfer',title: '委托转办原因:'
// },{
//   value: 7,label: '已阅,委派',eventType:'delegate',title: '委托委派原因:'
// },
// //   {
// //   //前置加签
// //   value: 8,label: '同意,先给其他人审批',eventType:'appendNodeBefore',title: '加签原因:'
// // },
//   {
//   //后置加签
//   value: 9,label: '同意,提交给下一个人审批',eventType:'appendNodeAfter',title: '加签原因:'
// }])

const authTypeListBuild = computed(() => {
  const authTypeListBuild = []
  for (let ele of authTypeList.value) {
    if(props.hasOperationData?.indexOf(ele.eventType)> -1){
      authTypeListBuild.push(ele);
    }
  }
  return authTypeListBuild;
})

const state = reactive({
  form: {
    authType: 1,
    remarks: undefined,
    rejectToNode: undefined
  },
});

watch(
    () => authTypeListBuild,
    (authTypeListBuild) => {
      state.form.authType = authTypeListBuild.value.length> 0? authTypeListBuild.value[0].value:undefined
      state.form.remarks = authTypeListBuild.value.length> 0?authTypeListBuild.value[0].label:undefined
      if(state.form.authType !== undefined){
        emits('update:eventType',authTypeListBuild.value[0].eventType);
      }
    },
    {
      immediate: true,
    }
);


const click = (index)=>{
  state.form.remarks = authTypeListBuild.value[index].label
  title.value = authTypeListBuild.value[index].title
  emits('update:eventType',authTypeListBuild.value[index].eventType);
}

const getData = ()=>{
  const result = {authRemarks: state.form.remarks,rejectToNode: state.form.rejectToNode}
  const nodeConfig = assigner.value?.getData()
  if(state.form.authType === 6){
    const transferInfo = transferOrDelegate.value?.getData()
    if(!transferInfo || transferInfo.setType !== 1){
      throw new Error('请选择转办人员');
    }else if(transferInfo.nodeUserList.length == 0){
      throw new Error('请选择转办人员');
    }else {
      const nodeUser = transferInfo.nodeUserList[0];
      result.transferInfo = {
        userId: nodeUser.id,
        fullName: nodeUser.name,
        remarks: state.form.remarks
      }
    }
  }else if(state.form.authType === 7){
    const delegateInfo = transferOrDelegate.value?.getData()
    if(!delegateInfo || delegateInfo.setType !== 1){
      // ElMessage.error('请选择委派人员');
      throw new Error('请选择委派人员');
    }else if(delegateInfo.nodeUserList.length == 0){
      throw new Error('请选择委派人员');
    }else {
      const nodeUser = delegateInfo.nodeUserList[0];
      result.delegateInfo = {
        userId: nodeUser.id,
        fullName: nodeUser.name,
        remarks: state.form.remarks
      }
    }
  }else if([8,9].indexOf(state.form.authType) > -1){
    const appendNodeForUser = appendNode.value?.getData()
    if(!appendNodeForUser || appendNodeForUser.setType !== 1){
      throw new Error('请选择审批人员');
    }else if(appendNodeForUser.nodeUserList.length == 0){
      throw new Error('请选择审批人员');
    }else {
      const nodeUser = appendNodeForUser.nodeUserList[0];
      result.appendNodeForUser = {
        userId: nodeUser.id,
        fullName: nodeUser.name,
        remarks: state.form.remarks
      }
    }
  }

  if(nodeConfig){
    result.taskForNodeConfig = nodeConfig
  }
  return result;
}

onMounted(() => {});

defineExpose({
  getData
})



</script>
